import { CopyControllerBase } from "./CopyControllerBase";
import { DataEvent } from "../../../events/DataEvent";
import { SPlayerGuildInfo } from "../../../../../Message/Game/SPlayerGuildInfo";
import { EGuildPosition } from "../../../../../Message/Game/EGuildPosition";
import { CopyUtil } from "../CopyUtil";
import { CopyConfig } from "../../../resource/CopyConfig";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { Language } from "../../../../../extend/language/Language";
import { GameProxy } from "../../../mvc/GameProxy";
import { MsgManager } from "../../../manager/MsgManager";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SGuildDefenseInfo } from "../../../../../Message/Public/SGuildDefenseInfo";
import { ECopyType } from "../../../../../Message/Public/ECopyType";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { SGuildDefenseCopyInfo } from "../../../../../Message/Public/SGuildDefenseCopyInfo";
import { SGuildDefenseStructure } from "../../../../../Message/Public/SGuildDefenseStructure";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { IView } from "../../../../mvc/interfaces/IView";
import { GuildCopyDefenseView } from "../guildCopyDefense/GuildCopyDefenseView";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
type int = number;
//class GuildCopyDefenseController
    
    export  class GuildCopyDefenseController extends CopyControllerBase
    {
        constructor()
        {
            super();
            return;
        }

        /* internal  */doOpenGuildDefense(arg1: DataEvent): void
        {
            var e: DataEvent;
            var info: SPlayerGuildInfo;
            var diff: int;
            var mode: string;
            var num: int;

            var loc1;
            diff = 0;
            mode = null;
            num = 0;
            e = arg1;
            info = cache.guild.myGuildInfo;
            if (info && (info.position == EGuildPosition._EGuildLeader || info.position == EGuildPosition._EGuildDeputyLeader)) 
            {
                diff = e.data as int;
                mode = CopyUtil.getDifficultyDef(diff);
                num = CopyConfig.instance.getGuildDefenseDiffPlayerNum(diff);
                Alert.show(Language.getStringByParam(60451, mode, num), Language.getString(69900), Alert.OK | Alert.CANCEL, null, (arg1: int): void
                {
                    if (arg1 == Alert.OK) 
                    {
                        GameProxy.copy.openGuildDefense(e.data as int);
                    }
                    return;
                })
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(60409));
            }
            return;
        }

        /* internal  */doEnterGuildDefense(arg1: DataEvent): void
        {
            GameProxy.copy.enterGuildDefense();
            return;
        }

        /* internal  */onReceiveDefenseInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SGuildDefenseInfo;
            cache.copy.guildDefenseInfo = loc1;
            if (!cache.copy.isInCopyType(ECopyType._ECopyGuildDefense)) 
            {
                if (loc1 && loc1.hasOpen) 
                {
                    this.defense.showOpenHintIcon();
                }
            }
            if (loc1 && loc1.todayOpen && !loc1.hasOpen) 
            {
                this.defense.hideOpenHintIcon();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyUpdateGuildCopyEntranceStatus));
            return;
        }

        /* internal  */onReceiveDefenseCopyInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SGuildDefenseCopyInfo;
            cache.copy.guildDefenseCopyInfo = loc1;
            this.defense.updateDefenseCopyInfo();
            return;
        }

        /* internal  */onDefenseStructureUpdate(arg1: MessageBlock): void
        {
            var loc2=null;
            var loc3=null;
            var loc1=arg1.messageBase as SGuildDefenseStructure;
            if (loc1.structure) 
            {
                loc2 = cache.copy.guildDefenseBuildDic;
                var loc4=0;
                var loc5=loc1.structure;
                for (loc3 in loc5) 
                {
                    loc2[loc3] = loc1.structure[loc3];
                }
                this.defense.updateDefenseBuildInfo();
            }
            return;
        }

        /* internal  */onShowDefenseInfoPanel(): void
        {
            var loc1=null;
            if (cache.copy.isInCopyType(ECopyType._ECopyGuildDefense)) 
            {
                this.defense.showDefenseInfoPanel();
                loc1 = cache.guild.myGuildInfo;
                if (loc1) 
                {
                    this.updateGuildPostion(loc1.position);
                }
                else 
                {
                    this.updateGuildPostion(-1);
                }
            }
            return;
        }

        /* internal  */hideDefenseInfoPanel(): void
        {
            if (this._defense) 
            {
                this.defense.hideDefenseInfoPanel();
            }
            return;
        }

        /* internal  */onContributeAdd(arg1: DataEvent): void
        {
            this._contribute = this._contribute + arg1.data;
            this.defense.updateDefenseContribute(this._contribute);
            return;
        }

        /* internal  */updateExp(arg1: int): void
        {
            this._exp = arg1;
            this.defense.updateDefenseExp(arg1);
            return;
        }

        /* internal  */doUpgradeBoss(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.copy.guildDefenseStructureUpgrade(loc1);
            return;
        }

        /* internal  */onUpdateBossRing(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            this.defense.updateDefenseBossRing(loc1);
            return;
        }

        /* internal  */onGuildPostionChange(arg1: DataEvent): void
        {
            this.updateGuildPostion(arg1.data as int);
            return;
        }

        /* internal  */updateGuildPostion(arg1: int): void
        {
            if (this._defense) 
            {
                this.defense.updateGuildPosition(arg1);
            }
            return;
        }

        public /* override */ enterCopy(arg1: TCopy): void
        {
            super.enterCopy(arg1);
            flash.utils.setTimeout(this.onShowDefenseInfoPanel, 1000);
            this.defense.hideOpenHintIcon();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildHideOpenCopyWindow));
            return;
        }

        public /* override */ leaveCopy(): void
        {
            if (this._exp > 0 || this._contribute > 0) 
            {
                Alert.show(Language.getStringByParam(60432, this._exp, this._contribute), Language.getString(69900));
            }
            super.leaveCopy();
            this.hideDefenseInfoPanel();
            cache.copy.guildDefenseBuildDic = new Map<any, any> /* flash.utils.Dictionary */();
            this._contribute = 0;
            this._exp = 0;
            return;
        }

        protected /* override */ addListener(): void
        {
            Dispatcher.addEventListener(EventName.GuildDefenseUpdateContribute, this.onContributeAdd);
            Dispatcher.addEventListener(EventName.GuildContrubuteAddByKillBoss, this.onContributeAdd);
            Dispatcher.addEventListener(EventName.GuildDefenseUpgradeBoss, this.doUpgradeBoss);
            Dispatcher.addEventListener(EventName.GuildDefenseUpdateRing, this.onUpdateBossRing);
            Dispatcher.addEventListener(EventName.GuildPostionChange, this.onGuildPostionChange);
            NetDispatcher.addCmdListener(ServerCommand.Copy_DefendCopyAddExp, this.updateExp);
            return;
        }

        protected /* override */ removeListener(): void
        {
            Dispatcher.removeEventListener(EventName.GuildDefenseUpdateContribute, this.onContributeAdd);
            Dispatcher.removeEventListener(EventName.GuildContrubuteAddByKillBoss, this.onContributeAdd);
            Dispatcher.removeEventListener(EventName.GuildDefenseUpgradeBoss, this.doUpgradeBoss);
            Dispatcher.removeEventListener(EventName.GuildDefenseUpdateRing, this.onUpdateBossRing);
            Dispatcher.removeEventListener(EventName.GuildPostionChange, this.onGuildPostionChange);
            NetDispatcher.removeCmdListener(ServerCommand.Copy_DefendCopyAddExp, this.updateExp);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._defense) 
            {
                this._defense = new GuildCopyDefenseView();
            }
            return this._defense;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.CopyOpenGuildDefense, this.doOpenGuildDefense);
            Dispatcher.addEventListener(EventName.CopyEnterGuildDefense, this.doEnterGuildDefense);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildDefenseInfo, this.onReceiveDefenseInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildDefenseCopyInfo, this.onReceiveDefenseCopyInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildDefenseStructureUpdate, this.onDefenseStructureUpdate);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildDefenseStructure, this.onDefenseStructureUpdate);
            NetDispatcher.addCmdListener(ServerCommand.GuildLeaveGuild, this.onLeaveGuild);
            return;
        }

        public /* override */ stageResize(): void
        {
            this.defense.stageResize();
            return;
        }

        /* internal  */onLeaveGuild(arg1): void
        {
            if (this._defense) 
            {
                this._defense.hideOpenHintIcon();
            }
            return;
        }

        public get defense(): GuildCopyDefenseView
        {
            if (!this._defense) 
            {
                this.initView();
            }
            return this._defense;
        }

        /* internal */ /* var */_defense: GuildCopyDefenseView;

        /* internal */ /* var */_contribute: int;

        /* internal */ /* var */_exp: int;
    }
